#pragma once
#include<iostream>

class Solution
{
private:
    int bitSum(int n)
    {
        int sum = 0;
        while (n)
        {
            sum += (n % 10) * (n % 10);
            n /= 10;
        }

        return sum;
    }

public:
    bool isHappy(int n)
    {
        int slow = n;
        int fast = bitSum(n);
        while (slow != fast)
        {
            slow = bitSum(slow);
            fast = bitSum(bitSum(fast));
        }

        return slow == 1;
    }
};